package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.AlipayUserMapper;
import com.net.xpay.core.domain.AlipayUser;
import com.net.xpay.core.manager.AlipayUserManager;
import com.net.xpay.core.model.AlipayUserSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;


@Component
@ReadTransactional
public class AlipayUserManagerImpl extends AbstractVersionedManager<AlipayUser> implements AlipayUserManager{

    @Autowired
    private AlipayUserMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AlipayUser obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, AlipayUser> getMapper() {
        return mapper;
    }

    @Override
    public AlipayUser findByIsvBizId(String isvBizId) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(isvBizId));
        AlipayUser condition = new AlipayUser();
        condition.setIsvBizId(isvBizId);
        return getByCondition(condition);
    }

    @Override
    public AlipayUser findByAlipayNickName(Long userId, String alipayNickName) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(alipayNickName));
        AlipayUser condition = new AlipayUser();
        condition.setUserId(userId);
        condition.setAlipayNickName(alipayNickName);
        return getByCondition(condition);
    }

    @Override
    public List<AlipayUser> list(AlipayUserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public List<AlipayUser> search(AlipayUserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AlipayUserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public AlipayUser findById(Long userId, Long alipayUserId) {
        Preconditions.checkArgument(userId != null && alipayUserId != null);
        AlipayUser condition = new AlipayUser();
        condition.setUserId(userId);
        condition.setId(alipayUserId);
        return getByCondition(condition);
    }

    @Override
    public AlipayUser findBySignBizNo(Long userId, String signBizNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(signBizNo));
        AlipayUser condition = new AlipayUser();
        condition.setUserId(userId);
        condition.setSignBizNo(signBizNo);
        return getByCondition(condition);
    }
}
